/**
 * 配置项管理初始化
 */
var DcConfig = {
    id: "DcConfigTable",    //表格id
    seItem: null,        //选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
DcConfig.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
//            {title: '主键', field: 'id', visible: true, align: 'center', valign: 'middle'},
//            {title: '应用id', field: 'appId', visible: true, align: 'center', valign: 'middle'},
            {title: '配置key', field: 'configKey', visible: true, align: 'center', valign: 'middle'},
            {title: '配置value', field: 'configValue', visible: true, align: 'center', valign: 'middle'},
            {title: '配置说明', field: 'configDesc', visible: true, align: 'center', valign: 'middle'},
            {title: '配置状态', field: 'statusDesc', visible: true, align: 'center', valign: 'middle'},
//            {title: '环境id', field: 'envId', visible: true, align: 'center', valign: 'middle'},
//            {title: '创建时间', field: 'createTime', visible: true, align: 'center', valign: 'middle'},
//            {title: '创建用户', field: 'createUser', visible: true, align: 'center', valign: 'middle'},
//            {title: '更新时间', field: 'updateTime', visible: true, align: 'center', valign: 'middle'},
//            {title: '更新用户', field: 'updateUser', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
DcConfig.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        DcConfig.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加配置项
 */
DcConfig.openAddDcConfig = function () {
    var index = layer.open({
        type: 2,
        title: '添加配置项',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/dcConfig/dcConfig_add?appId=' + $("#appId").val() +"&envId=" + $("#envId").val()
    });
    this.layerIndex = index;
};
/**
 * 点击上传
 */
DcConfig.upload = function () {
    var index = layer.open({
        type: 2,
        title: '配置上传',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/dcConfig/dcConfig_upload?appId=' + $("#appId").val() +"&envId=" + $("#envId").val()
    });
    this.layerIndex = index;
};
/**
 * 打开查看配置项详情
 */
DcConfig.openDcConfigDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '配置项详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/dcConfig/dcConfig_update/' + DcConfig.seItem.id
        });
        this.layerIndex = index;
    }
};
/**
 * 打开配置预览
 */
DcConfig.openDcConfigPreview = function () {
        var index = layer.open({
            type: 2,
            title: '配置项详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/dcConfig/dcConfig_preview?appId=' + $("#appId").val() +"&envId=" + $("#envId").val()
        });
};

/**
 * 打开父项目
 */
DcConfig.openDcConfigParent = function () {
        var index = layer.open({
            type: 2,
            title: '父项目列表',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/dcConfig/dcConfig_parent?appId=' + $("#appId").val() +"&envId=" + $("#envId").val()
        });
};
/**
 * 删除配置项
 */
DcConfig.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/dcConfig/delete", function (data) {
            if (data.code == 200){
                Feng.success("删除成功!");
                DcConfig.table.refresh();
            }else{
                Feng.error("删除失败!" + data.message + "!");
            }
        });
        ajax.set("dcConfigId",this.seItem.id);
         Feng.confirm("确认删除此条数据?",function(){
    	        ajax.start();
    	 })
    }
};

/**
 * 删除配置项
 */
DcConfig.recovery = function () {
	var ajax = new $ax(Feng.ctxPath + "/dcConfig/recovery", function (data) {
        if (data.code == 200){
            Feng.success("恢复成功!");
            DcConfig.table.refresh();
        }else{
            Feng.error("恢复失败!" + data.message + "!");
        }
    });
    ajax.set("appId",$("#appId").val());
    ajax.set("envId",$("#envId").val());
     Feng.confirm("确认恢复发布中配置，确认将删除未发布配置?",function(){
	        ajax.start();
	 })
};

/**
 * 删除配置项
 */
DcConfig.pub = function () {
	var ajax = new $ax(Feng.ctxPath + "/dcConfig/pub", function (data) {
        if (data.code == 200){
            Feng.success("发布成功!");
            DcConfig.table.refresh();
        }else{
            Feng.error("发布失败!" + data.message + "!");
        }
    });
    ajax.set("appId",$("#appId").val());
    ajax.set("envId",$("#envId").val());
     Feng.confirm("确认发布吗?",function(){
	        ajax.start();
	 })
};
DcConfig.appList = function() {
	var ajax = new $ax(Feng.ctxPath + "/dcConfig/appList", function (data) {
		 $.each(data, function(index, item) {
	            $("#appId").append(  //此处向select中循环绑定数据
	    "<option value="+item.id+">" + item.name+ "</option>");
	        });
	});
	ajax.start();
};
DcConfig.envList = function() {
	var ajax = new $ax(Feng.ctxPath + "/dcConfig/envList", function (data) {
		 $.each(data, function(index, item) {
	            $("#envId").append(  //此处向select中循环绑定数据
	    "<option value="+item.id+">" + item.name+ "</option>");
	        });
	});
	ajax.start();
};
DcConfig.formParams = function() {
    var queryData = {};
    queryData['appId'] = $("#appId").val();
    queryData['envId'] = $("#envId").val();
    queryData['key'] = $("#key").val();
    return queryData;
}
/**
 * 查询配置项列表
 */
DcConfig.search = function () {
    DcConfig.table.refresh({query: DcConfig.formParams()});
};
/**
 * 下载配置
 */
DcConfig.download = function () {
	window.location.href=Feng.ctxPath + "/dcConfig/download?appId=" + $("#appId").val() +"&envId=" + $("#envId").val()
};
$(function () {
	DcConfig.appList();
	DcConfig.envList();
    var defaultColunms = DcConfig.initColumn();
    var table = new BSTable(DcConfig.id, "/dcConfig/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(DcConfig.formParams());
    DcConfig.table = table.init();
    $('#appId').change(function(){  
      DcConfig.table.refresh({query: DcConfig.formParams()});
    })  
    $('#envId').change(function(){  
        DcConfig.table.refresh({query: DcConfig.formParams()});
    })  
    if ($('#appId').val()== null ||  $('#envId').val() == null){
    	$(":input").attr({"disabled":"disabled"});
    }
});
